package com.sskj.bibi.ui.fragment;


import android.support.v4.content.ContextCompat;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.bulong.rudeness.RudenessScreenHelper;
import com.shizhefei.mvc.MVCCoolHelper;
import com.shizhefei.mvc.MVCHelper;
import com.shizhefei.view.coolrefreshview.CoolRefreshView;
import com.sskj.bibi.R;
import com.sskj.bibi.R2;
import com.sskj.bibi.bean.RecordHistoryBean;
import com.sskj.bibi.bean.rxbus.HistoryEntrustBusBean;
import com.sskj.bibi.presenter.HistoryEntrustFragmentPresenter;
import com.sskj.common.adapter.ItemClickSupport;
import com.sskj.common.adapter.slimadapter.IViewInjector;
import com.sskj.common.adapter.slimadapter.SlimAdapter;
import com.sskj.common.adapter.slimadapter.SlimInjector;
import com.sskj.common.base.App;
import com.sskj.common.box.decoration.DividerLineItemDecoration;
import com.sskj.common.rxbus2.RxBus;
import com.sskj.common.rxbus2.Subscribe;
import com.sskj.common.rxbus2.ThreadMode;
import com.sskj.lib.Constans;
import com.sskj.lib.RConfig;
import com.sskj.lib.base.BaseFragment;
import com.sskj.lib.mvchelper.ModelRx2DataSource;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import butterknife.BindView;

@Route(path = RConfig.BIBI_HISTORY_ENTRUST)
public class HistoryEntrustFragment extends BaseFragment<HistoryEntrustFragmentPresenter> {

    @BindView(R2.id.recyclerView)
    RecyclerView recyclerView;
    @BindView(R2.id.coolRefreshView)
    CoolRefreshView coolRefreshView;

    String code;
    String type;

    private SlimAdapter slimAdapter;


    private MVCHelper mvcHelper;
    private HashMap<String, String> stateMap;


    @Override
    protected int getLayoutId() {
        return R.layout.bibi_activity_history_entrust;
    }

    @Override
    public HistoryEntrustFragmentPresenter getPresenter() {
        return new HistoryEntrustFragmentPresenter();
    }

    @Override
    protected void initView() {
        ARouter.getInstance().inject(this);
        RxBus.getDefault().register(this);

        stateMap = new HashMap<>();
        stateMap.put("0", App.INSTANCE.getString(R.string.bibihistoryEntrustFragment1));
        stateMap.put("1", App.INSTANCE.getString(R.string.bibihistoryEntrustFragment2));
        stateMap.put("2", App.INSTANCE.getString(R.string.bibirecordActivity6));
        stateMap.put("3", App.INSTANCE.getString(R.string.bibirecordActivity7));
        recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
        recyclerView.addItemDecoration(new DividerLineItemDecoration(getActivity())
                .setFirstDraw(false)
                .setLastDraw(false)
                .setPaintWidth((int) RudenessScreenHelper.pt2px(App.INSTANCE, 20))
                .setPaintColor(ContextCompat.getColor(App.INSTANCE, R.color.lib_bg))
        );
        slimAdapter = SlimAdapter.create().register(R.layout.bibi_recy_item_entrust_history, new SlimInjector<RecordHistoryBean>() {
            @Override
            public void onInject(RecordHistoryBean data, IViewInjector injector, List<Object> payloads) {
                injector.text(R.id.entrust_type, data.isBuy() ? App.INSTANCE.getString(R.string.bibitipBibiUtil1) : App.INSTANCE.getString(R.string.bibirecordActivity5))
                        .textColor(R.id.entrust_type, ContextCompat.getColor(App.INSTANCE, data.isBuy() ? R.color.bibi_03c : R.color.bibiRedText))
                        .text(R.id.entrust_name, data.getLCode() + "/" + data.getRCode())
                        .text(R.id.entrust_time, data.getDealTime())
                        .text(R.id.entrust_prePrice, data.isMarket() ? App.INSTANCE.getString(R.string.bibihistoryEntrustFragment7) : data.getEntrustPrice())
                        .text(R.id.entrust_prePrice_text, String.format(App.INSTANCE.getString(R.string.bibiallEntrustFragment4), data.getRCode()))
                        .text(R.id.entrust_totalPrice, data.getDealTotalPrice())
                        .text(R.id.entrust_totalPrice_text, String.format(App.INSTANCE.getString(R.string.bibirecordDetailActivity2), data.getRCode()))
                        .text(R.id.entrust_realNum, data.getDealsRemainNum())
                        .text(R.id.entrust_realNum_text, String.format(App.INSTANCE.getString(R.string.bibirecordDetailActivity5), data.getLCode()))
                        .text(R.id.entrust_realPrice, data.getDealPrice())
                        .text(R.id.entrust_realPrice_text, String.format(App.INSTANCE.getString(R.string.bibirecordDetailActivity4), data.getRCode()))
                        .text(R.id.entrust_state, stateMap.get(data.getState() + "") + " >");
                if (data.isBuyAndMarket()) {  //市价买入
                    injector.text(R.id.entrust_preNum_text, String.format(App.INSTANCE.getString(R.string.bibihistoryEntrustFragment12), data.getRCode()))
                            .text(R.id.entrust_preNum, data.getTotalPrice());
                } else { //其他
                    injector.text(R.id.entrust_preNum_text, String.format(App.INSTANCE.getString(R.string.bibiallEntrustFragment6), data.getLCode()))
                            .text(R.id.entrust_preNum, data.getTotalNum());
                }
            }
        }).attachTo(recyclerView);
        slimAdapter.updateData(new ArrayList());

        mvcHelper = new MVCCoolHelper<>(coolRefreshView);
        ModelRx2DataSource refreshDataSource = new ModelRx2DataSource<RecordHistoryBean>(
                (ModelRx2DataSource.OnLoadSource<RecordHistoryBean>)
                        page -> mPresenter.getEntrustHistoryFlow(page + "", "10", code, type), 10);
        mvcHelper.setDataSource(refreshDataSource);
        mvcHelper.setAdapter(slimAdapter);
        ItemClickSupport.addTo(recyclerView)
                .setOnItemClickListener((recyclerView1, i, view) -> {
                    RecordHistoryBean dataItem = (RecordHistoryBean) slimAdapter.getDataItem(i);
                    if (dataItem.getState() == 2) {
                        ARouter.getInstance()
                                .build(RConfig.BIBI_RECORD_DETAIL)
                                .withString(Constans.ID, dataItem.getId())
                                .withString(Constans.TRADE_TYPE, dataItem.getTradeType() + "")
                                .navigation();
                    }
                });
    }

    @Override
    protected void initData() {
        mvcHelper.refresh();
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void refresh(HistoryEntrustBusBean allEntrustBusBean) {
        code = allEntrustBusBean.getCode();
        type = allEntrustBusBean.getType();
        mvcHelper.refresh();

    }

}
